
/**
* 部队基类
*/
class ArmyBase extends egret.DisplayObjectContainer {
    public constructor() {
        super();
        this.touchEnabled = true;
        this.initView();
        this.initEvent();
    }

    private mcf: egret.MovieClipDataFactory;
    /** 动画 */
    public mc: egret.MovieClip;
    /** 血条 */
    private _haemalStrand: egret.DisplayObjectContainer;
    /** 血条长度 */
    private _haemalStrandW: number = 40;
    private num: number = 0;
    private rect: egret.Rectangle = new egret.Rectangle();
    private _data: ArmyInfo;
    private _path: egret.Point[] = [];
	
    private initView(): void {
        var data = RES.getRes("army_json");
        var tex = RES.getRes("army_png");
        this.mcf = new egret.MovieClipDataFactory(data,tex);
        this.mc = new egret.MovieClip(this.mcf.generateMovieClipData("stand0"));
        this.addChild(this.mc);

        this._haemalStrand = new egret.DisplayObjectContainer();
        for(var i: number = 0;i < this._haemalStrandW;i++) {
            var bitmap = new egret.Bitmap();
            bitmap.texture = RES.getRes("blood");//从精灵表中获取某一项
            bitmap.x = i;
            this._haemalStrand.addChild(bitmap);
        }
        var rtt: egret.RenderTexture = new egret.RenderTexture();
        rtt.drawToTexture(this._haemalStrand,this._haemalStrand.getBounds());
        var bmp: egret.Bitmap = new egret.Bitmap(rtt);
        this._haemalStrand.removeChildren();
        this._haemalStrand.addChild(bmp);
        this._haemalStrand.x = 3;
        this._haemalStrand.y = -5;
        this.rect = new egret.Rectangle(0,0,this._haemalStrandW * this.num,3);
        this._haemalStrand.mask = this.rect;
        this.addChild(this._haemalStrand);
            
    }
    private initEvent(): void {
        this.addEventListener(egret.Event.ENTER_FRAME,this.moveDelay,this);
    }
    private removeEvent(): void {
        this.removeEventListener(egret.Event.ENTER_FRAME,this.moveDelay,this);
    }

    public get data(): ArmyInfo {
        return this._data;
    }
    public set data(value: ArmyInfo) {
        this._data = value;
        this.refreshView();
    }
    private refreshView(): void {
		if(this.data.skinNmae=="army"){
			this.data.isloaded = true;
			this.refestSkin();
		}else{
			RES.addEventListener(RES.ResourceEvent.GROUP_COMPLETE,this.onResourceLoadComplete,this);
			RES.addEventListener(RES.ResourceEvent.GROUP_LOAD_ERROR,this.onResourceLoadError,this);
			RES.loadGroup(this.data.skinNmae);
		}
    }
    private onResourceLoadComplete(event: RES.ResourceEvent): void {
        if(event.groupName == this.data.skinNmae) {
            this.data.isloaded = true;
            this.refestSkin();
        }
    }
    private onResourceLoadError(event: RES.ResourceEvent): void {
        this.onResourceLoadComplete(event);
    }
    public refestSkin(): void {
        if(!this.data.isloaded) return;
        var datastr: string = "army" + this.data.lifeType + "_json";
        var texstr: string = this.data.skinNmae + this.data.lifeType + "_png";
        var data = RES.getRes(datastr);
        var tex = RES.getRes(texstr);
        this.mcf = new egret.MovieClipDataFactory(data,tex);
        this.mc.movieClipData = this.mcf.generateMovieClipData("stand" + this.data.direction);
                    
        if(this.data.ismove) {
            this.play();
        }
        else {
            this.stop();
        }
    }
                
    /**
    * 播放状态
    */
    public play(): void {
        this.data.ismove = true;
        if(this.data.lifeType == "") {
            this.mc.play(-1);
        }
        else {
            this.mc.play();
        }
    }
    public stop(): void {
        this.data.ismove = false;
        this.mc.stop();
    }
    /**
    * 生存状态
    */
    public normal(): void {
        if(this.data.lifeType == "") return;
        this.data.lifeType = "";
        this.refestSkin();
    }
    public dead(): void {
        if(this.data.lifeType == "_dead") return;
        this.data.lifeType = "_dead";
        this.refestSkin();
    }
    public attack(): void {
        if(this.data.lifeType == "_melee") return;
        this.data.lifeType = "_melee";
        this.refestSkin();
    }
    /**
    * 受伤
    */
    public hit(num: number): void {
        this.data.bloodVolume -= num;
        this.blood();
    }
    /**
    * 加血
    */
    public addBlood(num: number): void {
        this.data.bloodVolume += num;
        this.blood();
    }
    /**
    * 血条
    */
    private blood(): void {
        this.num = this.data.bloodVolume / this.data.bloodTotal;
        this.rect = new egret.Rectangle(0,0,this._haemalStrandW * this.num,3);
        this._haemalStrand.mask = this.rect;
    }
    /**
    * 移动
    */
    public move(path: egret.Point[]): void {
        this._path = path;
        if(this._path.length == 0) return;
        this.play();
        this.addEventListener(egret.Event.ENTER_FRAME,this.moveDelay,this);
    }
    private moveDelay(): void {
        if(this._path.length == 0) return;
        this.setDirection(this._path[0]);
        //下一个节点
        var point: egret.Point = this._path[0];
        //根据两点距离，我们算出在特定时间速度所需要移动的距离。
        var targetSpeed: egret.Point = CommonFunction.GetSpeed(point,new egret.Point(this.x,this.y),this.data.speed);
        var xDistance: number = targetSpeed.x;
        var yDistance: number = targetSpeed.y;
        //成立 与目标距离少于速度
        if(Math.abs(point.x - this.x) <= Math.abs(xDistance) && Math.abs(point.y - this.y) <= Math.abs(yDistance)) {
            this.x = point.x;
            this.y = point.y;
            this._path.shift();
                            
            //成立 最后一个位移坐标
            if(this._path.length == 0) {
                if(this.data.lifeType != "_melee") {
                    this.stop();
                }
                this.removeEventListener(egret.Event.ENTER_FRAME,this.moveDelay,this);
                return;
            }
            else {

            }
        }
        else {
            this.x = this.x + xDistance;
            this.y = this.y + yDistance;
        }
    }
    /*   设定朝向，朝向发生变        
     * @param p:准备去的下一
     */
    public setDirection(_p: egret.Point) {
        var dzX: number = Math.abs(_p.x - (this.x));
        var dzY: number = Math.abs(_p.y - (this.y));
        //if (Math.abs(dzY - dzX) < 0.0001) return;
        var tempDirection: number;
        if(dzY > dzX) {
            if(_p.y < (this.y)) {
                tempDirection = 3;
            }
            else {
                tempDirection = 0;
            }
        }
        else {
            if(_p.x < (this.x)) {
                tempDirection = 1;
            }
            else {
                tempDirection = 2;
            }
        }
        if(tempDirection != this.data.direction) {
            this.data.direction = tempDirection;
            this.refestSkin();
        }
    }
    /**
    * 方向
    */
    public down(): void {
        this.data.direction = 0;
        this.refestSkin();
    }
    public left(): void {
        this.data.direction = 1;
        this.refestSkin();
    }
    public right(): void {
        this.data.direction = 2;
        this.refestSkin();
    }
    public up(): void {
        this.data.direction = 3;
        this.refestSkin();
    }
    /**
    * 销毁
    */
    public dispose(): void {
        this.removeEvent();
        this.removeChild(this.mc);
        this.removeChild(this._haemalStrand);
    }
	
	
	
	
	
	
	
	
	
}